package loengud.w09;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class ButtonExampleFrame extends JFrame {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private JLabel lbl;
	class HelloWorldButtonListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println(e);
			String command = e.getActionCommand();
			if (command == null) {
				// NULL check is needed
				// otherwise we could get NullPointerException
				
				// command is null, let's just return
				return;
			}
			if (command.equals("hello")) {
				doSomethingWhenClicked();
			} else {
				lbl.setText("maailm!");
			}
		}
	}
	
	public ButtonExampleFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(300, 300);
		getContentPane().setLayout(new FlowLayout());
		
		JButton button1 = new JButton("hello");
		// let's add listener
		button1.addActionListener(new HelloWorldButtonListener());
		getContentPane().add(button1);
		
		JButton button2 = new JButton("tere");
		button2.addActionListener(new HelloWorldButtonListener());
		getContentPane().add(button2);
		
		lbl = new JLabel("tere");
		getContentPane().add(lbl);
	}
	
	private void doSomethingWhenClicked() {
		// do something when button is clicked
		lbl.setText("World!");
	}
	public static void main(String[] args) {
		ButtonExampleFrame f = new ButtonExampleFrame();
		f.setVisible(true);
	}
}
